﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ITSTOREBLL;
using ITStore.ViewModels;
using PagedList;

namespace ITStore.Controllers
{ 
    public class TinTucController : BaseController
    {
        private ITStoreEntities db = new ITStoreEntities();

        //
        // GET: /TinTuc/

        public ViewResult Index(int? page)
        {
            var baiviets = db.BaiViets.Include("LoaiBaiViet");
            int pageSize = 20;
            int pageNumber = (page ?? 1);
            return View(baiviets.OrderByDescending(bv => bv.bv_ID).ToPagedList(pageNumber, pageSize));
        }

        //
        // GET: /TinTuc/Details/5
        [ValidateInput(false)]
        public ViewResult Details(int id,string content,int? page)
        {
            BaiViet baiviet = db.BaiViets.Single(b => b.bv_ID == id);
            if (!string.IsNullOrEmpty(content))
            {
                BinhLuanBaiViet blbv = new BinhLuanBaiViet();
                blbv.blbv_BaiViet = baiviet.bv_ID;
                blbv.blbv_NoiDung = content;
                blbv.blbv_NgayDang = System.DateTime.Now;
                blbv.blbv_NguoiDang = 1;
                db.BinhLuanBaiViets.AddObject(blbv);
                db.SaveChanges();
            }
            int pageNumber = (page ?? 1);
            int pageSize = 5;
            int pageresult = ((pageNumber * pageSize) - pageSize);
            List<BinhLuanBaiViet> dsBinhLuanBaiViet = (db.BinhLuanBaiViets.Where(bl => bl.blbv_BaiViet == baiviet.bv_ID).OrderByDescending(bl => bl.blbv_NgayDang).Skip(pageresult).Take(pageSize)).ToList();
            List<BinhLuanBaiViet> countBinhLuanBaiViet = (db.BinhLuanBaiViets.Where(bl => bl.blbv_BaiViet == baiviet.bv_ID).OrderBy(bl => bl.blbv_NgayDang)).ToList();
            int count = countBinhLuanBaiViet.Count();
            int countmax = (count / pageSize) + 1;
            ChiTietBaiVietViewModel ctbv = new ChiTietBaiVietViewModel() { ChiTietBV = baiviet, DanhSachBinhLuan = dsBinhLuanBaiViet,PageCount = countmax,PageNumber = pageNumber};
            return View(ctbv);
        }

        //
        // GET: /TinTuc/Create

        public ActionResult Create()
        {
            ViewBag.bv_Loai = new SelectList(db.LoaiBaiViets, "lbv_ID", "lbv_Loai");
            return View();
        } 

        //
        // POST: /TinTuc/Create

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Create(BaiViet baiviet,string content)
        {
            if (ModelState.IsValid)
            {
                baiviet.bd_Ngay = System.DateTime.Now;
                baiviet.bv_NoiDung = content;
                db.BaiViets.AddObject(baiviet);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.bv_Loai = new SelectList(db.LoaiBaiViets, "lbv_ID", "lbv_Loai", baiviet.bv_Loai);
            return View(baiviet);
        }
        
        //
        // GET: /TinTuc/Edit/5
 
        public ActionResult Edit(int id)
        {
            BaiViet baiviet = db.BaiViets.Single(b => b.bv_ID == id);
            ViewBag.bv_Loai = new SelectList(db.LoaiBaiViets, "lbv_ID", "lbv_Loai", baiviet.bv_Loai);
            return View(baiviet);
        }

        //
        // POST: /TinTuc/Edit/5

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Edit(BaiViet baiviet,string content)
        {
            if (ModelState.IsValid)
            {
                baiviet.bv_NoiDung = content;
                baiviet.bd_Ngay = System.DateTime.Now;
                db.BaiViets.Attach(baiviet);
                db.ObjectStateManager.ChangeObjectState(baiviet, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.bv_Loai = new SelectList(db.LoaiBaiViets, "lbv_ID", "lbv_Loai", baiviet.bv_Loai);
            return View(baiviet);
        }

        //
        // GET: /TinTuc/Delete/5
 
        public ActionResult Delete(int id)
        {
            BaiViet baiviet = db.BaiViets.Single(b => b.bv_ID == id);
            return View(baiviet);
        }

        public ActionResult DeleteBinhLuan(int id)
        {
            BinhLuanBaiViet BLBaiViet = db.BinhLuanBaiViets.Single(b => b.blbv_ID == id);
            return View(BLBaiViet);
        }

        //
        // POST: /TinTuc/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            BaiViet baiviet = db.BaiViets.Single(b => b.bv_ID == id);
            db.BaiViets.DeleteObject(baiviet);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        [HttpPost, ActionName("DeleteBinhLuan")]
        public ActionResult DeleteBinhLuanConfirmed(int id)
        {
            BinhLuanBaiViet BLBaiViet = db.BinhLuanBaiViets.Single(b => b.blbv_ID == id);
            int idbaiviet = BLBaiViet.blbv_BaiViet;
            db.BinhLuanBaiViets.DeleteObject(BLBaiViet);
            db.SaveChanges();
            return RedirectToAction("Details/"+idbaiviet);
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

        public ViewResult TinMoi()
        {
            var tinmoi = from tm in db.BaiViets
                         select tm;
            tinmoi = tinmoi.OrderByDescending(s => s.bv_ID);
            int pageSize = 5;
            int pageNumber = (1);
            return View(tinmoi.ToPagedList(pageNumber, pageSize));
        }
    }
}